package com.whfc.wxmp.api.base.service.impl;

import com.whfc.base.dto.MsgRecordDTO;
import com.whfc.base.dto.MsgStatisticsDTO;
import com.whfc.base.param.WarnMsgSearchParam;
import com.whfc.base.service.MessageService;
import com.whfc.common.enums.AppWarnModuleType;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.PageVO;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.AssertUtil;
import com.whfc.emp.enums.AppEmpWarnRuleType;
import com.whfc.entity.dto.msg.AppWarnMsgDTO;
import com.whfc.env.enums.EnvWarnRuleType;
import com.whfc.fim.enums.FimDeviceRuleType;
import com.whfc.fse.enums.FseWarnRuleType;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.service.SysUserService;
import com.whfc.mach.enums.AppMachWarnRuleType;
import com.whfc.quality.dto.QualityCheckDTO;
import com.whfc.quality.dto.QualityCheckDescDTO;
import com.whfc.quality.enums.QualityCheckUserType;
import com.whfc.quality.param.QualityCheckListParam;
import com.whfc.quality.service.QualityCheckService;
import com.whfc.safety.dto.SafetyCheckDTO;
import com.whfc.safety.dto.SafetyCheckDescDTO;
import com.whfc.safety.enums.SafetyCheckUserType;
import com.whfc.safety.param.SafetyCheckListParam;
import com.whfc.safety.service.SafetyCheckService;
import com.whfc.wxmp.api.base.service.WxMessageService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/11/30 9:10
 */
@Service
public class WxMessageServiceImpl implements WxMessageService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @DubboReference(interfaceClass = MessageService.class, version = "1.0.0",timeout = 100000)
    private MessageService messageService;
    @DubboReference(interfaceClass = SysUserService.class, version = "1.0.0")
    private SysUserService sysUserService;

    @DubboReference(interfaceClass = QualityCheckService.class, version = "1.0.0")
    private QualityCheckService qualityCheckService;

    @DubboReference(interfaceClass = SafetyCheckService.class, version = "1.0.0")
    private SafetyCheckService safetyCheckService;

    @Override
    public ListData<AppWarnMsgDTO> getWarnGeneral(Integer deptId, String phone) {
        return messageService.getWarnGeneral(deptId, phone);
    }

    @Override
    public PageData<AppWarnMsgDTO> getWarnMsgList(Integer deptId, String phone, Integer handleState, Integer moduleType, Integer pageNum, Integer pageSize) {

        PageData<AppWarnMsgDTO> pageData = messageService.getWarnMsgList(deptId, phone, handleState, moduleType, pageNum, pageSize);
        for (AppWarnMsgDTO appWarnMsgDTO : pageData.getList()) {
            String pic;
            String title = appWarnMsgDTO.getTitle();
            AppWarnModuleType appWarnModuleType = AppWarnModuleType.parseValue(appWarnMsgDTO.getModuleType());
            switch (appWarnModuleType) {
                case MACH:
                    pic = AppMachWarnRuleType.parseTittle(title).pic();
                    break;
                case EMP:
                    pic = AppEmpWarnRuleType.parseByTittle(title).pic();
                    break;
                case FSE:
                    pic = FseWarnRuleType.parseByTittle(title).pic();
                    break;
                case FIM:
                    pic = FimDeviceRuleType.parseByTittle(title).pic();
                    break;
                case ENV:
                    pic = EnvWarnRuleType.parseTittle(title).pic();
                    break;
                default:
                    pic = "";
            }
            appWarnMsgDTO.setPic(pic);
        }
        return pageData;
    }

    @Override
    public void setMsgRead(List<Integer> idList) {
        if (idList != null && idList.size() > 0) {
            messageService.read(idList);
        }
    }

    @Override
    public void delMsg(List<Integer> idList) {
        if (idList != null && idList.size() > 0) {
            messageService.del(idList);
        }
    }

    @Override
    public PageData<AppWarnMsgDTO> list(String userPhone, Integer pageNum, Integer pageSize, Integer deptId, Integer state, Integer moduleType) {
        return messageService.list(userPhone, pageNum, pageSize, deptId, state, moduleType);
    }

    @Override
    public Map getNewApproveWithMe(String phone, Integer deptId) {
        SysUser sysUser = sysUserService.getUserByPhone(phone);
        // 质量
        QualityCheckListParam checkListParam = new QualityCheckListParam();
        checkListParam.setUserId(sysUser.getId());
        checkListParam.setPageSize(1);
        checkListParam.setPageNum(1);
        checkListParam.setDeptId(deptId);
        Integer[] type = {QualityCheckUserType.RECTIFIER.getValue(), QualityCheckUserType.REVIEWER.getValue(), QualityCheckUserType.VERIFIER.getValue()};
        checkListParam.setType(type);
        List<QualityCheckDTO> qualityCheckList = qualityCheckService.list(checkListParam).getCheckList();
        List<Integer> checkIds = qualityCheckList.stream().map(QualityCheckDTO::getCheckId).collect(Collectors.toList());
        List<QualityCheckDescDTO> descList = qualityCheckService.descByIds(checkIds);

        // 安全
        SafetyCheckListParam safetyCheckListParam = new SafetyCheckListParam();
        Integer[] safetyType = {SafetyCheckUserType.RECTIFIER.getValue(), SafetyCheckUserType.REVIEWER.getValue(), SafetyCheckUserType.VERIFIER.getValue()};
        safetyCheckListParam.setUserId(sysUser.getId());
        safetyCheckListParam.setPageSize(1);
        safetyCheckListParam.setPageNum(1);
        safetyCheckListParam.setDeptId(deptId);
        safetyCheckListParam.setType(safetyType);
        List<SafetyCheckDTO> safetyCheckList = safetyCheckService.list(safetyCheckListParam).getCheckList();
        List<Integer> safetyCheckIds = safetyCheckList.stream().map(SafetyCheckDTO::getCheckId).collect(Collectors.toList());
        List<SafetyCheckDescDTO>  safetyDescList = safetyCheckService.descByIds(safetyCheckIds);

        HashMap result = new HashMap<>(3);
        result.put("count",descList.size()+safetyDescList.size());
        result.put("qualityList",descList);
        result.put("safetyList",safetyDescList);
        return result;
    }

    @Override
    public List<MsgStatisticsDTO> getWarnMsgStatistics(Integer deptId, Integer warnType, Integer userId) {
        return messageService.getWarnMsgStatistics(deptId, warnType, userId);
    }

    @Override
    public PageVO<MsgRecordDTO> getWarnMsgPage(WarnMsgSearchParam param) {
        return messageService.getWarnMsgPage(param);
    }

}
